package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

//@Component
public class SpringTaskPersistence {

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService detailService;

    /**
     * 以0秒开始计算,每隔五秒执行一次.
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void  persistence(){
        //1.把数据从Redis中查询出来
        List<StrategyStats> vos= strategyStatsCacheService.getAllStrategyStats();
        for (StrategyStats ss : vos) {
            //2.把数据封装成detail对象
            StrategyDetail detail = new StrategyDetail();
            detail.setId(ss.getStrategyId());
            detail.setViewnum(ss.getViewnum());
            detail.setFavornum(ss.getFavornum());
            detail.setReplynum(ss.getReplynum());
            detail.setThumbsupnum(ss.getThumbsupnum());
            //3.更行到mysql
            detailService.updateStats(detail);
        }

    }

}
